﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;
namespace NetCoreCMS.Web.Areas.Admin.Controllers.Article
{
    public partial class Spec_EditController : ManageController
    {
        private const string WEB_VIEW = "~/Areas/Admin/Views/Article/spec_edit.cshtml";
        private string action = CMSEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;
        public IArticle_specService _article_spec { get; set; }
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            ActionResult result = View(EDIT_RESULT_VIEW);
            string _action = Request.Query["action"];

            int.TryParse(Request.Query["id"], out this.id);
            if (!string.IsNullOrEmpty(_action) && _action == CMSEnums.ActionEnum.Edit.ToString())
            {
                this.action = CMSEnums.ActionEnum.Edit.ToString();//修改类型
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    filterContext.Result = result;
                    return;
                }
                if (!_article_spec.Exists(this.id))
                {
                    JscriptMsg("记录不存在或已删除！", "back");
                    filterContext.Result = result;
                    return;
                }
            }
            ViewBag.Action = this.action;
            ViewBag.Id = this.id.ToString();
        }

        //
        // GET: /admin/Spec_Edit/
        public ActionResult Index()
        {
            ChkAdminLevel("article_2_spec", CMSEnums.ActionEnum.View.ToString()); //检查权限
            if (action == CMSEnums.ActionEnum.Edit.ToString())
            {
                ShowInfo(this.id);
            }
            else
            {
                Article_spec model = new Article_spec();
                model.sort_id = 99;
                ViewData["model"] = model;
            }
            return View(WEB_VIEW);
        }

        //保存
        public ActionResult SubmitSave()
        {
            if (action == CMSEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("article_2_spec", CMSEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误！", "");
                }
                else
                {
                    JscriptMsg("修改规格成功！", "../spec_list/index");
                }
            }
            else //添加
            {
                ChkAdminLevel("article_spec", CMSEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", "");
                }
                else
                {
                    JscriptMsg("添加规格成功！", "../spec_list/index");
                }
            }
            return View(EDIT_RESULT_VIEW);
        }

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            Article_spec model = _article_spec.GetModel(_id);
            ViewData["model"] = model;
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            bool result = false;
            Article_spec model = new Article_spec();
            model.title = Request.Form["txtTitle"].FirstOrDefault().Trim();
            model.remark = Request.Form["txtRemark"].FirstOrDefault().Trim();
            model.sort_id = Utils.StrToInt(Request.Form["txtSortId"].FirstOrDefault().Trim(), 99);

            #region 保存规格选项
            string[] itemIdArr = Request.Form["item_id"];
            string[] itemTitleArr = Request.Form["item_title"];
            string[] itemImgUrlArr = Request.Form["item_imgurl"];
            string[] itemSortIdArr = Request.Form["item_sortid"];
            if (itemIdArr != null && itemTitleArr != null && itemImgUrlArr != null && itemSortIdArr != null)
            {
                if ((itemIdArr.Length == itemTitleArr.Length) && (itemTitleArr.Length == itemImgUrlArr.Length) && (itemImgUrlArr.Length == itemSortIdArr.Length))
                {
                    List<Article_spec_value> ls = new List<Article_spec_value>();
                    for (int i = 0; i < itemIdArr.Length; i++)
                    {
                        Article_spec_value modelt =new Article_spec_value();
                        modelt.id = Utils.StrToInt(itemIdArr[i].Trim(), 0);
                        modelt.title = itemTitleArr[i].Trim();
                        modelt.img_url = itemImgUrlArr[i].Trim();
                        modelt.sort_id = Utils.StrToInt(itemSortIdArr[i].Trim(), 99);
                        ls.Add(modelt);
                    }
                    model.values = ls;
                }
            }
            #endregion

            if (_article_spec.Add(model) > 0)
            {
                AddAdminLog(CMSEnums.ActionEnum.Add.ToString(), "添加商品规格:" + model.title); //记录日志
                result = true;
            }
            return result;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = false;
            Article_spec model = _article_spec.GetModel(_id);

            model.title = Request.Form["txtTitle"].FirstOrDefault().Trim();
            model.remark = Request.Form["txtRemark"].FirstOrDefault().Trim();
            model.sort_id = Utils.StrToInt(Request.Form["txtSortId"].FirstOrDefault().Trim(), 99);

            #region 保存规格选项
            string[] itemIdArr = Request.Form["item_id"];
            string[] itemTitleArr = Request.Form["item_title"];
            string[] itemImgUrlArr = Request.Form["item_imgurl"];
            string[] itemSortIdArr = Request.Form["item_sortid"];
            if (itemIdArr != null && itemTitleArr != null && itemImgUrlArr != null && itemSortIdArr != null)
            {
                if ((itemIdArr.Length == itemTitleArr.Length) && (itemTitleArr.Length == itemImgUrlArr.Length) && (itemImgUrlArr.Length == itemSortIdArr.Length))
                {
                    List<Article_spec_value> ls = new List<Article_spec_value>();
                    for (int i = 0; i < itemIdArr.Length; i++)
                    {
                        Article_spec_value modelt = new Article_spec_value();
                        modelt.id = Utils.StrToInt(itemIdArr[i].Trim(), 0);
                        modelt.parent_id = model.id;
                        modelt.title = itemTitleArr[i].Trim();
                        modelt.img_url = itemImgUrlArr[i].Trim();
                        modelt.sort_id = Utils.StrToInt(itemSortIdArr[i].Trim(), 99);
                        ls.Add(modelt);
                    }
                    model.values = ls;
                }
            }
            #endregion

            if (_article_spec.Update(model))
            {
                AddAdminLog(CMSEnums.ActionEnum.Edit.ToString(), "修改商品规格:" + model.title); //记录日志
                result = true;
            }
            return result;
        }
        #endregion
    }
}
